#!/bin/bash

source common/common.sh common

function action_upgrade_feeds()
{
  cd $op_dir
  local fn
  [ -z "$feeds_name" ] && feeds_name="all"
  [ "$feeds_name" == "-" -o "$feeds_name" == "c" ] && feeds_name="commonpack"
  [ "$feeds_name" == "all" ] && fn="-a" || fn="$feeds_name"
  show i $(lang "upgrade feeds: $feeds_name" "升级feeds仓库: $feeds_name")
  $doProxy ./scripts/feeds update "$fn"
  $doProxy ./scripts/feeds install -a
  #"$fn"
  exit 0
}

function action_upgrade_custom_feeds()
{
  feeds_name="-"
  action_upgrade_feeds
  exit 0
}

function action_use_proxy()
{
  if [ $use_proxy == true ]; then
    local has_proxy=$(netstat -apnl 2>/dev/null | grep $proxy_port)
    [ ! -z "$has_proxy" ] && ( doProxy="proxychains"; \
      show s $(lang "used ssh proxy($proxy_port)" "检测到ssh代理服务端口($proxy_port)")) || \
      show s $(lang "no ssh proxy" "没有ssh代理服务")
    doProxy="proxychains"
  fi
  doAction=true
}

function action_verbose()
{
  doVerbose="V=s"
}

function action_install_new_openwrt()
{
  show i $(lang "install openwrt base form git" "安装openwrt基本平台")
  cd $op_root
  ./openwrt_synchro -f
  exit 0
}

function action_reset_current_openwrt()
{
  show i $(lang "reinstall openwrt base form git" "重新安装openwrt基本平台")
  cd $op_root
  ./openwrt_synchro -m ch -f
  exit 0
}

function action_install_release_openwrt()
{
  show i $(lang "install openwrt base form git(release version)" "重新安装openwrt基本平台(release版本)")
  cd $op_root
  ./openwrt_synchro -m rh -f
  exit 0
}

function action_install_beta_openwrt()
{
  show i $(lang "install openwrt base form git(beta version)" "重新安装openwrt基本平台(beta版本)")
  cd $op_root
  ./openwrt_synchro -m bh -f
  exit 0
}

function action_download_package()
{
  show i $(lang "download all package source code" "下载所选模块所有源码包")
  cd $op_dir
  if [ $download_proxy_package == true ]; then
    svpn.sh -r
    use_proxy=true
    action_use_proxy
  fi
  $doProxy make download $doVerbose $doMakeJ
  exit 0
}

function action_restart_tftp()
{
  show i $(lang "restart tftp server" "启动tftp服务")
  sudo service tftpd-hpa restart
  exit 0
}

function action_make_usb_image_disk()
{
  show i $(lang "copy firmware file to usb driver" "拷贝产生的固件文件到usb盘")
  cd $op_root
  local bn=$(./hardware_manage -gv bn)
  local bt=$(./hardware_manage -gv bt)
  local pf=$(./hardware_manage -gv pf)
  local cn=$(./hardware_manage -gv cn)
  local tr=$(./hardware_manage -gv tr)
  local tl=$(./hardware_manage -gv tl)
  local bf=$(./hardware_manage -gv bf)
  local ud=$(./hardware_manage -gv ud)
  local mnt=$ud
  local src="$op_dir/bin/$pf/$bf"
  #local nbf=$(basename $bf .bin)"_"$(now).bin
  #local mnt=$(df -h | grep $ud | awk '{print $6}')
  if [ ! -e "$ud" ]; then
    show s $(lang "usb disk($ud) not exists" "目标usb盘($ud)不存在")
    exit 0
  fi
  #local dst="$mnt/$nbf"
  #echo $src
  #echo $dst
  #echo $link
  local fsize
  local fsizeh
  local fdtime
  local fhash
  if [ -f "$src" ]; then
    [ -f "$dst" ] && rm $dst
    fsize=$(ls -l $src | awk '{print $5}')
    fsizeh=$(ls -lh $src | awk '{print $5}')
    fhash=$(md5sum -b $src | awk '{print $1}')
    show s $(lang "new file" "拷贝文件：") $ud
    lang " filesize: $fsize byte ($fsizeh) md5:$fhash" " 文件大小: $fsize 字节 ($fsizeh) md5:$fhash"
    sudo dd if=$src of=$ud conv=fsync
    #sync
  else
    show s $(lang "no file copy." "没有文件被复制")
  fi
  exit 0
}

function action_copy_usb_image_disk()
{
  show i $(lang "copy firmware file to usb directory" "拷贝产生的固件文件到usb目录")
  cd $op_root
  local bn=$(./hardware_manage -gv bn)
  local bt=$(./hardware_manage -gv bt)
  local pf=$(./hardware_manage -gv pf)
  local cn=$(./hardware_manage -gv cn)
  local tr=$(./hardware_manage -gv tr)
  local tl=$(./hardware_manage -gv tl)
  local bf=$(./hardware_manage -gv bf)
  local ud=$(./hardware_manage -gv ud)
  local src="$op_dir/bin/$pf/$bf"
  local nbf=$(basename $bf .bin)"_"$(now).bin
  local mnt=$(df -h | grep $ud | awk '{print $6}')
  if [ ! -d "$mnt" ]; then
    show s $(lang "usb directory($ud) not exists" "目标usb目录($ud)不存在")
    exit 0
  fi
  local dst="$mnt/$nbf"
  #echo $src
  #echo $dst
  #echo $link
  local fsize
  local fsizeh
  local fdtime
  local fhash
  if [ -f "$src" ]; then
    [ -f "$dst" ] && rm $dst
    fsize=$(ls -l $src | awk '{print $5}')
    fsizeh=$(ls -lh $src | awk '{print $5}')
    fhash=$(md5sum -b $src | awk '{print $1}')
    fdtime=$(stat -c %y $src | awk -F. '{print $1}')
    show s $(lang "new file" "拷贝文件：") $dst " ($fdtime)"
    lang " filesize: $fsize byte ($fsizeh) md5:$fhash" " 文件大小: $fsize 字节 ($fsizeh) md5:$fhash"
    cp -f $src $dst
    sync
  else
    show s $(lang "no file copy." "没有文件被复制")
  fi
  exit 0
}

function action_clear()
{
  show i $(lang "clear project" "清除项目")
  cd $op_dir
  if [ $clear_temp == true ]; then
    rm -rf ./tmp
  else
    make clean $doVerbose $doMakeJ
  fi
  exit 0
}

function action_backup_config()
{
  show i $(lang "backup openwrt config" "备份openwrt配置")
  [ -z "$backup_config" ] && exit 0
  cd $op_dir
  show s $(lang "copy .config to config.$backup_config" "拷贝 .config 为 config.$backup_config")
  cp .config config.$backup_config
}

function action_recover_config()
{
  show i $(lang "recover openwrt config" "恢复openwrt配置")
  [ -z "$recover_config" ] && exit 0
  cd $op_dir
  show s $(lang "copy config.$backup_config to .config" "拷贝 config.$backup_config 为 .config")
  cp config.$recover_config .config
}

function action_show_diff_config()
{
  show i $(lang "show openwrt config diff" "显示openwrt配置差异")
  cd $op_dir
  local difftool=$(which bcompare)
  local prefix="config."
  local pp1=".config"
  local pp2=".config"
  local ppmode="-rupN"
  [ ! -f "$difftool" -o "$pmode" != "-" ] && difftool=diff
  #[ -z "$pmode" -o "$pmode" == "--" ] || prefix=""
  [ -z "$pmode" -o "$pmode" == "-" -o "$pmode" == "--" ] || ppmode=$pmode
  [ -z "$p1" -o "$p1" == "-" ] || pp1=$prefix$p1
  [ -z "$p2" -o "$p2" == "-" ] || pp2=$prefix$p2
  #[ "$difftool" == "diff" ] || ppmode="&"
  echo "command: $difftool $pp1 $pp2 $ppmode"
  $difftool $pp1 $pp2 $ppmode
}

function action_build()
{
  show i $(lang "build openwrt" "编译openwrt")
  cd $op_dir
  if [ -z "$build_j" ]; then
    make defconfig
    $doProxy make $doVerbose $doMakeJ
  else
    make defconfig
    $doProxy make -j $build_j -l9 $doVerbose
  fi
}

function action_build_image()
{
  show i $(lang "build openwrt image" "仅编译openwrt镜像")
  cd $op_dir
  if [ ! -z "$package_name" ]; then
    $doProxy make package/$package_name/compile $doVerbose $doMakeJ
    $doProxy make package/$package_name/install $doVerbose $doMakeJ
  fi
  $doProxy make package/install $doVerbose $doMakeJ
  $doProxy make target/install $doVerbose $doMakeJ
  $doProxy make package/index $doVerbose $doMakeJ
}

function action_copy_tftp()
{
  show i $(lang "copy firmware file to tftp root directory" "拷贝产生的固件文件到tftp的固件目录")
  cd $op_root
  local bn=$(./hardware_manage -gv bn)
  local bt=$(./hardware_manage -gv bt)
  local pf=$(./hardware_manage -gv pf)
  local cn=$(./hardware_manage -gv cn)
  local tr=$(./hardware_manage -gv tr)
  local tl=$(./hardware_manage -gv tl)
  local bf=$(./hardware_manage -gv bf)
  local src="$op_dir/bin/$pf/$bf"
  local dst="$tr/$bf"
  local link="$tr/$tl"
  #echo $src
  #echo $dst
  #echo $link
  local fsize
  local fsizeh
  local ofsize
  local ofsizeh
  local ofhash
  local ofdtime
  local fdtime
  local fhash
  #exit 0
  if [ -L "$link" ]; then
    ofsize=$(ls -l $tr/$(basename $(readlink $link)) | awk '{print $5}')
    ofsizeh=$(ls -lh $tr/$(basename $(readlink $link)) | awk '{print $5}')
    ofhash=$(md5sum -b $tr/$(basename $(readlink $link)) | awk '{print $1}')
    ofdtime=$(stat -c %y $tr/$(basename $(readlink $link)) | awk -F. '{print $1}')
    show s $(lang "old file" "原 文 件：") $(basename $(readlink $link)) "=>" $tl " ($ofdtime)"
    lang " filesize: $ofsize byte ($ofsizeh) md5:$ofhash" " 文件大小: $ofsize 字节 ($ofsizeh) md5:$ofhash"
    echo "link $tl exists! rm $tl file!"
    rm -f $link
  fi
  if [ -f "$src" ]; then
    [ -f "$dst" ] && rm $dst
    fsize=$(ls -l $src | awk '{print $5}')
    fsizeh=$(ls -lh $src | awk '{print $5}')
    fhash=$(md5sum -b $src | awk '{print $1}')
    fdtime=$(stat -c %y $src | awk -F. '{print $1}')
    show s $(lang "new file" "生成文件：") $tl "=>" $bf " ($fdtime)"
    lang " filesize: $fsize byte ($fsizeh) md5:$fhash" " 文件大小: $fsize 字节 ($fsizeh) md5:$fhash"
    [ ! -z "$ofhash" -a ! -z "$fhash" -a "$ofhash" != "$fhash" ] && show s $(lang "file chanage!" "文件发生变更")
    [ ! -z "$ofhash" -a ! -z "$fhash" -a "$ofhash" == "$fhash" ] && show s $(lang "file no chanage!" "文件一致")
    cp -f $src $dst
    ln -s $(basename $dst) $link
  else
    show s $(lang "no file copy." "没有文件被复制")
  fi
}

function action_make_package()
{
  show i $(lang "make package" "编译package")
  cd $op_dir
  [ -z "$package_name" ] && exit 0
  local pn="compile"
  [ $make_package == true ] && pn="compile"
  [ $make_package_install == true ] && pn="install"
  [ $make_package_download == true ] && pn="download"
  [ $make_package_clean == true ] && pn="chean"
  [ $make_package_refresh == true ] && pn="refresh"
  $doProxy make package/$package_name/$pn $doVerbose $doMakeJ
  exit 0
}

function action_make_menu()
{
  show i $(lang "make menuconfig" "执行menuconfig(openwrt功能定制)")
  cd $op_dir
  make menuconfig $doVerbose
  exit 0
}

function action_make_kernel_menu()
{
  show i $(lang "make kernel menuconfig" "执行kernel menuconfig(linux内核定制)")
  cd $op_dir
  make kernel_menuconfig $doVerbose
  exit 0
}

function action_make_defconfig()
{
  show i $(lang "make defconfig" "执行Openwrt配置依赖关系梳理（清除无效配置，检测并设置依赖关系）")
  cd $op_dir
  make defconfig $doVerbose
  exit 0
}

function action_make_j()
{
  [ ! -z "$make_j" ] && doMakeJ="" || doMakeJ="-j $make_j -l9"
}

function action_patch_openwrt()
{
  show i $(lang "patch project(app)" "给当前openwrt打上项目应用补丁")
  [ -z "$proj_name" ] && exit 0
  cd $op_root
  local board_type=$(./hardware_manage -gv pf)
  $doProxy ./patch_openwrt.py proj -t $board_type $proj_name -w
}

function action_patch_platform()
{
  show i $(lang "patch project(platform)" "给当前openwrt打上项目平台补丁")
  cd $op_root
  local board_type=$(./hardware_manage -gv pf)
  local board_name=$(./hardware_manage -gv bn)
  $doProxy ./patch_openwrt.py proj -t $board_type $board_name -w -P
}

function action_patch_host()
{
  show i $(lang "patch project(host)" "给当前openwrt打上项目交叉编译补丁")
  cd $op_root
  $doProxy ./patch_openwrt.py proj -H -w empty
}

doAction=false
op_root=$(get_wrtoy_path)
op_wrtoy_dir=$(get_wrtoy_path ..)
op_hp_dir=$(get_wrtoy_path profiles)
op_tl_dir=$(get_wrtoy_path ../tftproot)
op_dir=$(get_wrtoy_path ../openwrt)
doProxy=
proxy_port=7070
doVerbose=
doMakeJ=

#======================================================
help_feeds=$(lang \
  "upgrade and install feeds" \
  "升级并安装feeds(默认为安装全部feeds)")

addOption -f    --feeds \
    action=action_upgrade_feeds dest=feeds_name \
    help="$help_feeds"

#======================================================
help_custom_feeds=$(lang \
  "upgrade and install custom feeds (feeds: commonpack)" \
  "升级并安装commonpack feeds仓库")

addOption -fc   --feeds-commonpack \
    flagTrue action=action_upgrade_custom_feeds dest=feeds_custom \
    help="$help_custom_feeds"

#======================================================
help_use_proxy=$(lang \
  "use sock5 proxy(proxychains support)" \
  "使用sock5代理(proxychains支持)")

addOption -pc   --proxychains \
    flagTrue action=action_use_proxy dest=use_proxy \
    help="$help_use_proxy"

#======================================================
help_install_new_openwrt=$(lang \
  "install new Git source from Openwrt" \
  "从Openwrt的Git中提取最新的源")

addOption -I --install-openwrt \
    flagTrue action=action_install_new_openwrt dest=install_new_openwrt \
    help="$help_install_new_openwrt"

#======================================================
help_reset_current_openwrt=$(lang \
  "reset current Git source from Openwrt" \
  "从Openwrt的Git中提取当前版本的源")

addOption -R --reset-openwrt \
    flagTrue action=action_reset_current_openwrt dest=reset_current_openwrt \
    help="$help_reset_current_openwrt"

#======================================================
help_install_release_openwrt=$(lang \
  "install release version Git source from Openwrt" \
  "从Openwrt的Git中提取项目release版本的源")

addOption -IR --install-release-openwrt \
    flagTrue action=action_install_release_openwrt dest=install_release_openwrt \
    help="$help_install_release_openwrt"

#======================================================
help_install_beta_openwrt=$(lang \
  "install beta version Git source from Openwrt" \
  "从Openwrt的Git中提取项目beta版本的源")

addOption -IB --install-beta-openwrt \
    flagTrue action=action_install_beta_openwrt dest=install_beta_openwrt \
    help="$help_install_beta_openwrt"

#======================================================
help_verbose=$(lang \
  "show verbose" \
  "显示更详细的信息")

addOption -v --verbose \
    flagTrue action=action_verbose dest=verbose \
    help="$help_verbose"

#======================================================
help_download=$(lang \
  "download all package source code" \
  "下载所有选中的package的源码")

help_downloadp=$(lang \
  "download all package source code from sock5 proxy" \
  "通过sock5代理下载所有选中的package的源码")

addOption -dl --download \
    flagTrue dest=download_package \
    help="$help_download"

addOption -dl+ --download-proxy \
    flagTrue dest=download_proxy_package \
    help="$help_download"

print $download_proxy_package

#======================================================
help_backup_config=$(lang \
  "backup openwrt config" \
  "备份openwrt配置")

addOption -bc --backup-config \
    action=action_backup_config dest=backup_config \
    help="$help_backup_config"

#======================================================
help_recover_config=$(lang \
  "recover openwrt config" \
  "恢复openwrt配置")

addOption -rc --recover-config \
    action=action_recover_config dest=recover_config \
    help="$help_recover_config"

#======================================================
help_show_diff_config=$(lang \
  "show diff openwrt config" \
  "显示openwrt配置差异")

addOption -dc --diff-config \
    flagTrue dest=diff_config \
    help="$help_show_diff_config"

#======================================================
help_show_diff_config_mode=$(lang \
  "show diff openwrt config mode" \
  "显示openwrt配置差异模式")

addOption -pm --patch-mode \
    default="-" dest=pmode \
    help="$help_show_diff_config_mode"

#======================================================
help_show_diff_config_p1=$(lang \
  "show diff openwrt config file1" \
  "openwrt配置文件1")

addOption -p1 --patch-file1 \
    default="-" dest=p1 \
    help="$help_show_diff_config_p1"

#======================================================
help_show_diff_config_p2=$(lang \
  "show diff openwrt config file2" \
  "openwrt配置文件2")

addOption -p2 --patch-file2 \
    default="-" dest=p2 \
    help="$help_show_diff_config_p2"

#======================================================
help_clear=$(lang \
  "clear project" \
  "清除项目数据")

help_cleart=$(lang \
  "clear project tmp dir" \
  "清除项目菜单缓存目录")

addOption -cl --clear \
    flagTrue action=action_clear dest=clear\
    help="$help_clear"

addOption -ct --clear-temp \
    flagTrue action=action_clear dest=clear_temp \
    help="$help_cleart"

#======================================================
help_make_usb_image_disk=$(lang \
  "make firmware image file to usbdisk(image mode)" \
  "将指定u盘制作成启动盘(x86/rpi)")

addOption -iu   --usb \
    flagTrue action=action_make_usb_image_disk dest=usb_image\
    help="$help_make_usb_image_disk"

#======================================================
help_copy_usb_image_disk=$(lang \
  "copy firmware image file to usbdisk(copy mode)" \
  "将镜像拷贝到指定u盘")

addOption -cu   --usb-copy \
    flagTrue action=action_copy_usb_image_disk dest=usb_copy\
    help="$help_copy_usb_image_disk"

#======================================================
help_restart_tftp=$(lang \
  "restart tftp server" \
  "启动tftp服务")

addOption --tftp \
    flagTrue action=action_restart_tftp dest=restart_tftp \
    help="$help_restart_tftp"

#======================================================

help_build_image=$(lang \
  "build openwrt image" \
  "编译openwrt镜像")

addOption -bi   --build-image \
    flagTrue dest=build_image \
    help="$help_build_image"

#======================================================
help_build=$(lang \
  "build openwrt" \
  "编译openwrt")

help_buildj=$(lang \
  "build openwrt -j" \
  "多线程编译openwrt")

addOption -b   --build \
    flagTrue dest=build_base \
    help="$help_build"

addOption -bj   --build-j \
    dest=build_j\
    help="$help_buildj"

#======================================================
help_copy=$(lang \
  "copy firmware file to tftp root directory" \
  "拷贝产生的固件文件到tftp的固件目录")

addOption -c   --copy \
    flagTrue dest=copy_tftp \
    help="$help_copy"

#======================================================
help_package_name=$(lang \
  "set package name" \
  "设置package名称")

help_make_package=$(lang \
  "make package" \
  "编译package")

help_make_packagei=$(lang \
  "install package" \
  "安装package")

help_make_packaged=$(lang \
  "download package source" \
  "下载package源码")

help_make_packagec=$(lang \
  "clean package" \
  "清除package编译文件")

help_make_packager=$(lang \
  "refresh package" \
  "刷新package")

addOption -pn   --package-name \
    dest=package_name \
    help="$help_package_name"

addOption -mp   --make-package \
    flagTrue dest=make_package \
    help="$help_make_package"

addOption -mpi   --make-package-install \
    flagTrue dest=make_package_install \
    help="$help_make_packagei"

addOption -mpd   --make-package-download \
    flagTrue dest=make_package_download \
    help="$help_make_packaged"

addOption -mpc   --make-package-clean \
    flagTrue dest=make_package_clean \
    help="$help_make_packagec"

addOption -mpr   --make-package-refresh \
    flagTrue dest=make_package_refresh \
    help="$help_make_packager"

#======================================================
help_make_j=$(lang \
  "multi thread compiled" \
  "执行多线程编译")

addOption -j \
    action=action_make_j dest=make_j \
    help="$help_make_menu_j"

#======================================================
help_make_menu=$(lang \
  "make menuconfig" \
  "执行menuconfig(openwrt功能定制)")

addOption -m   --menu-config \
    flagTrue action=action_make_menu dest=menu_config\
    help="$help_make_menu"

#======================================================
help_make_kernel_menuconfig=$(lang \
  "make kernel menuconfig" \
  "执行kernel menuconfig(linux内核定制)")

addOption -km   --kernel-menu-config \
    flagTrue action=action_make_kernel_menu dest=kernel_menu_config\
    help="$help_make_kernel_menuconfig"

#======================================================
help_make_defconfig=$(lang \
  "make defconfig" \
  "执行Openwrt配置依赖关系梳理（清除无效配置，检测并设置依赖关系）")

addOption -def   --default-config \
    flagTrue action=action_make_defconfig  dest=make_defconfig\
    help="$help_make_defconfig"

#======================================================
help_patch_openwrt=$(lang \
  "patch project(app)" \
  "给当前openwrt打上项目应用补丁")

addOption -po   --patch-openwrt \
    default=mtall action=action_patch_openwrt dest=proj_name \
    help="$help_patch_openwrt"

#======================================================
help_patch_platform=$(lang \
  "patch project(platform)" \
  "给当前openwrt打上项目平台补丁")

addOption -pp   --patch-platform \
    flagTrue action=action_patch_platform dest=patch_platform\
    help="$help_patch_platform"

#======================================================
help_patch_host=$(lang \
  "patch project(host)" \
  "给当前openwrt打上项目交叉编译平台补丁")

addOption -ph   --patch-host \
    flagTrue action=action_patch_host dest=patch_host\
    help="$help_patch_host"

#addOption -P --platform \
#    action=action_reset_last_openwrt \
#    help="set platform"

parseOptions "$@"

#action_run $profile $debug
function action_run()
{
  [ $diff_config == true ] && action_show_diff_config
  [ $download_package == true -o $download_proxy_package == true ] && action_download_package
  [ $make_package == true -o \
    $make_package_install == true -o \
    $make_package_download == true -o \
    $make_package_clean == true -o \
    $make_package_refresh == true  ] && action_make_package
  [ $build_base == true ] && action_build
  [ $build_image == true ] && action_build_image
  [ $build_j ] && action_build
  [ $copy_tftp == true ] && action_copy_tftp
}

action_run